﻿Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Class RayTraceShader
    Inherits ShaderBase
    Protected vshader As Integer
    Protected fshader As Integer

    Protected Shared self As RayTraceShader
    Public Shared ReadOnly Property Singleton As RayTraceShader
        Get
            Return self
        End Get
    End Property

    Protected Sub New(vs As String, fs As String)
        program = GL.CreateProgram()
        vshader = GL.CreateShader(ShaderType.VertexShader)
        fshader = GL.CreateShader(ShaderType.FragmentShader)

        GL.ShaderSource(vshader, vs)
        GL.CompileShader(vshader)
        Dim vsLog As String = GL.GetShaderInfoLog(vshader)
        If vsLog.Contains("error") Then Throw New Exception(vsLog)

        GL.ShaderSource(fshader, fs)
        GL.CompileShader(fshader)
        Dim fsLog As String = GL.GetShaderInfoLog(fshader)
        If fsLog.Contains("error") Then Throw New Exception(fsLog)
        GL.AttachShader(program, vshader)
        GL.AttachShader(program, fshader)
        GL.LinkProgram(program)
        Dim programLog As String = GL.GetProgramInfoLog(program)
        If programLog.Contains("error") Then Throw New Exception(programLog)
    End Sub


    Public Shared Sub Load(vs As String, fs As String)
        self = New RayTraceShader(vs, fs)
    End Sub

End Class

Class TransformFeedbackShader
    Inherits ShaderBase
    Protected Shared self As TransformFeedbackShader
    Public Shared ReadOnly Property Singleton As TransformFeedbackShader
        Get
            Return self
        End Get
    End Property
    Protected vshader As Integer
    Protected Sub New(vs As String)
        program = GL.CreateProgram()
        vshader = GL.CreateShader(ShaderType.VertexShader)

        GL.ShaderSource(vshader, vs)
        GL.CompileShader(vshader)
        Dim vsLog As String = GL.GetShaderInfoLog(vshader)
        If vsLog.Contains("error") Then Throw New Exception(vsLog)
        GL.AttachShader(program, vshader)
        GL.LinkProgram(program)
        Dim programLog As String = GL.GetProgramInfoLog(program)
        If programLog.Contains("error") Then Throw New Exception(programLog)
    End Sub

    Public Shared Sub Load(vs As String)
        self = New TransformFeedbackShader(vs)
    End Sub
End Class

Class GbufferShader
    Inherits ShaderBase
    Protected Shared self As GbufferShader
    Public Shared ReadOnly Property Singleton As GbufferShader
        Get
            Return self
        End Get
    End Property
    Protected vshader As Integer
    Protected gshader As Integer
    Protected fshader As Integer

    Protected Sub New(vs As String, fs As String, Optional gs As String = Nothing)
        program = GL.CreateProgram()
        vshader = GL.CreateShader(ShaderType.VertexShader)
        If gs IsNot Nothing Then
            gshader = GL.CreateShader(ShaderType.GeometryShader)
        End If
        fshader = GL.CreateShader(ShaderType.FragmentShader)

        GL.ShaderSource(vshader, vs)
        GL.CompileShader(vshader)
        Dim vsLog As String = GL.GetShaderInfoLog(vshader)
        If vsLog.Contains("error") Then Throw New Exception(vsLog)

        If gs IsNot Nothing Then
            GL.ShaderSource(gshader, gs)
            GL.CompileShader(gshader)
            Dim gsLog As String = GL.GetShaderInfoLog(gshader)
            If gsLog.Contains("error") Then Throw New Exception(gsLog)
        End If

        GL.ShaderSource(fshader, fs)
        GL.CompileShader(fshader)
        Dim fsLog As String = GL.GetShaderInfoLog(fshader)
        If fsLog.Contains("error") Then Throw New Exception(fsLog)

        GL.AttachShader(program, vshader)
        If gs IsNot Nothing Then
            GL.AttachShader(program, gshader)
        End If
        GL.AttachShader(program, fshader)
        GL.LinkProgram(program)
        Dim programLog As String = GL.GetProgramInfoLog(program)
        If programLog.Contains("error") Then Throw New Exception(programLog)
    End Sub

    Public Shared Sub Load(vs As String, fs As String, Optional gs As String = Nothing)
        self = New GbufferShader(vs, fs, gs)
    End Sub

End Class

Class ShadowMapShader
    Inherits ShaderBase
    Protected vshader As Integer
    Protected fshader As Integer

    Protected Shared self As ShadowMapShader
    Public Shared ReadOnly Property Singleton As ShadowMapShader
        Get
            Return self
        End Get
    End Property

    Protected Sub New(vs As String, fs As String)
        program = GL.CreateProgram()
        vshader = GL.CreateShader(ShaderType.VertexShader)
        fshader = GL.CreateShader(ShaderType.FragmentShader)

        GL.ShaderSource(vshader, vs)
        GL.CompileShader(vshader)
        Dim vsLog As String = GL.GetShaderInfoLog(vshader)
        If vsLog.Contains("error") Then Throw New Exception(vsLog)

        GL.ShaderSource(fshader, fs)
        GL.CompileShader(fshader)
        Dim fsLog As String = GL.GetShaderInfoLog(fshader)
        If fsLog.Contains("error") Then Throw New Exception(fsLog)
        GL.AttachShader(program, vshader)
        GL.AttachShader(program, fshader)
        GL.LinkProgram(program)
        Dim programLog As String = GL.GetProgramInfoLog(program)
        If programLog.Contains("error") Then Throw New Exception(programLog)
    End Sub


    Public Shared Sub Load(vs As String, fs As String)
        self = New ShadowMapShader(vs, fs)
    End Sub
End Class
